package com.ancy.misc;

import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.ConnectivityManager;
import android.telephony.TelephonyManager;

import com.ancy.manager.InternetManager;

public class Utils {
	public void CopyStream(InputStream is, OutputStream os) {
		final int buffer_size = 1024;
		try {
			byte[] bytes = new byte[buffer_size];
			for (;;) {
				int count = is.read(bytes, 0, buffer_size);
				if (count == -1)
					break;
				os.write(bytes, 0, count);
			}
		} catch (Exception ex) {
		}
	}

	/** This Function is for Split the RGB Values */

	public static int rgbSpliter(String RGBValue) {
		String[] splited_values = RGBValue.split(",");
		return Color.rgb(Integer.parseInt(splited_values[0]),
				Integer.parseInt(splited_values[1]),
				Integer.parseInt(splited_values[2]));
	}

	public Bitmap getBitmapData(String url) {
		Bitmap bmp = null;
		try {
			InternetManager call = new InternetManager(url);
			byte[] data = call.fileRequest();
			bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
			data = null;
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
		return bmp;
	}

	public static boolean checkInternet(Context context) {
		ConnectivityManager connec = (ConnectivityManager) context
				.getSystemService(context.CONNECTIVITY_SERVICE);

		if (connec.getNetworkInfo(0).isConnectedOrConnecting()
				|| connec.getNetworkInfo(1).isConnectedOrConnecting()) {

			return true;
		} else {

			return false;
		}
	}

	public static void alertBox(Context context, String msg) {
		AlertDialog.Builder alert = new AlertDialog.Builder(context);
		alert.setTitle("MobileCMS");
		alert.setMessage(msg);
		alert.setNeutralButton("OK", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
			}
		});
		alert.show();
	}

	public static String getDeviceId(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String device_id = telephonyManager.getDeviceId();
		return device_id;
	}

	public static Date getDateFromString(String dateString) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date dateTime = new Date();
		try {
			dateTime = format.parse(dateString);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			dateTime = Calendar.getInstance().getTime();
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
		return dateTime;
	}

	public static String getFriendlyTime(String dateString) {

		Date dateTime = getDateFromString(dateString);

		StringBuffer sb = new StringBuffer();
		//Date current = GlobalConstants.CURRENT_TIME;
		Date current = null;
		long diffInSeconds = (current.getTime() - dateTime.getTime()) / 1000;
		long sec = (diffInSeconds >= 60 ? diffInSeconds % 60 : diffInSeconds);
		long min = (diffInSeconds = (diffInSeconds / 60)) >= 60 ? diffInSeconds % 60
				: diffInSeconds;
		long hrs = (diffInSeconds = (diffInSeconds / 60)) >= 24 ? diffInSeconds % 24
				: diffInSeconds;
		long days = (diffInSeconds = (diffInSeconds / 24)) >= 30 ? diffInSeconds % 30
				: diffInSeconds;
		long months = (diffInSeconds = (diffInSeconds / 30)) >= 12 ? diffInSeconds % 12
				: diffInSeconds;
		long years = (diffInSeconds = (diffInSeconds / 12));

		if (years > 0) {
			if (years == 1) {
				sb.append("a year");
			} else {
				sb.append(years + " years");
			}
		} else if (months > 0) {
			if (months == 1) {
				sb.append("last month");
			} else {
				sb.append(months + " months");
			}
		} else if (days > 0) {
			if (days == 1) {
				sb.append("yesterday");
			} else {
				sb.append(days + " days");
			}
		} else if (hrs > 0) {
			if (hrs == 1) {
				sb.append("an hour");
			} else {
				sb.append(hrs + " hours");
			}
		} else if (min > 0) {
			if (min == 1) {
				sb.append("a minute");
			} else {
				sb.append(min + " minutes");
			}
		} else {
			if (sec <= 1) {
				sb.append("about a second");
			} else {
				sb.append("about " + sec + " seconds");
			}
		}

		if (days != 1 && months != 1) {
			sb.append(" ago");
		}
		return sb.toString();
	}

	public static String getCalDate(String dateString) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date dateTime = new Date();
		try {
			dateTime = format.parse(dateString);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dateTime.toLocaleString();
	}

	public static String getMonth(String dateString) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		DateFormat outFormat = new SimpleDateFormat("MMM");
		Date dateTime = new Date();
		try {
			dateTime = format.parse(dateString);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return outFormat.format(dateTime);
	}

	public static String getDate(String dateString) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		DateFormat outFormat = new SimpleDateFormat("dd");
		Date dateTime = new Date();
		try {
			dateTime = format.parse(dateString);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return outFormat.format(dateTime);
	}

	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);
		final float roundPx = pixels;

		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;
	}

	public static Bitmap getShadowBitmap(Bitmap bitmap) {
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		Paint mShadow = new Paint();
		// radius=10, y-offset=2, color=black
		mShadow.setShadowLayer(30.0f, 0.0f, 3.0f, 0xFF000000);
		// in onDraw(Canvas)
		canvas.drawBitmap(bitmap, 0.0f, 0.0f, mShadow);
		return output;
	}
}